Skip to content

Clear the stack before CppiaAdoptInstances counts what a module keeps - #507

Open
dimensionscape wants to merge 1 commit into
mainfrom
fix/cppia-adopt-instances-stack
Open

dimensionscape wants to merge 1 commit into
mainfrom
fix/cppia-adopt-instances-stack

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

test/regression/CppiaAdoptInstances failed once in the ASan job on Linux, on #505, whose changes are all in the build tool:

jit true: the old module keeps only what stayed: Reshaped x1, closures x2

stdout.txt expects closures x1, the bound method that stays. The next line, and is freed once they go: "", was right, so nothing leaked. The same runtime passed that job on other runs, including one at b6981ce.

Why

The census collection behind __scriptable_cppia_retained scans the frames it runs in for pointers, as every collection does. A slot that nothing has written yet still holds what an earlier call left there. When that is the address of an object freed since, whose memory a closure of the old module now uses, the census counts the closure. cce2fed runs every script call on a joined worker, so the workers' values never reach the main thread's stack, but the main thread's own leftovers below its frame can still sit under retained()'s frames.

Planting the address of a dropped closure of the old module across 64 KB of stack below main's frame, just before the count, reproduces the CI line exactly, in both the interpreted and the JIT pass.

The change

main clears 64 KB of stack below its frame before each count, so every frame the census scans starts from zeros. main's own frame is scanned at every collection, so what it points to is never freed and cannot turn into a closure. Counting on a fresh worker instead would not do: glibc hands out the stacks of earlier threads from its cache without clearing them, and those threads ran the script code. Test only, so no Changes.md line.

Tests

On Windows x64 with MSVC 14.50 and Haxe 4.3.7:

  • The test matches stdout.txt in 40 runs, and in 25 under -D HXCPP_SANITIZE=address. The old test also passed 25 times under ASan here, as Windows lays the stack out differently, so the Linux ASan job is the real check.
  • With the dropped closure's address planted below main's frame, the old test prints closures x2, and this one matches stdout.txt.
  • Keeping a second closure of the old module for real still fails the test, with closures x2 and and is freed once they go: "Reshaped x1, closures x1".

CppiaAdoptInstances failed once in the ASan job on Linux, on a pull
request whose changes were all in the build tool, with "the old module
keeps only what stayed: Reshaped x1, closures x2" in the JIT pass, where
one closure stays. The next count, after the old values are dropped,
was right, so nothing leaked. The census collection that counts what a
released module keeps scans the frames it runs in for pointers, and a
slot that nothing has written yet still holds what an earlier call left
there. When that is the address of an object freed since, whose memory
a closure of the old module now uses, the census counts the closure.
Running every script call on a joined worker (cce2fed) kept the
workers' values off the main thread's stack, but not the main thread's
own leftovers.

Planting the address of a dropped closure of the old module across the
stack below main's frame before the count reproduces the line exactly,
in both passes. main now clears 64 KB of stack below its frame before
each count, so the frames the census scans start from zeros. main's own
frame is scanned at every collection, so what it points to is never
freed and cannot become a closure.

Tests: on Windows x64 with MSVC 14.50 and Haxe 4.3.7, CppiaAdoptInstances
matches stdout.txt in 40 runs, and in 25 under -D HXCPP_SANITIZE=address,
where the old test also passed 25 times. With the dropped closure's
address planted below main's frame, the old test prints closures x2 and
the new one matches. Keeping a second closure of the old module for
real still fails it, with closures x2 and the module not freed. The
Linux ASan job has not run it yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant